package main

func removeDuplicates(nums []int) int {
	if len(nums) <= 1 {
		return len(nums)
	}
	count := 1
	current := nums[0]
	for i := 1; i < len(nums); i++ {
		val := nums[i]
		if val != current { // 连续相同数字结束
			if count > 2 {
				beg := i - count + 2
				copy(nums[beg:], nums[i:])
				nums = nums[:len(nums)-count+2]
				i = i - count + 2
			}
			current = val
			count = 1
		} else {
			count++
		}
	}
	if count > 2 {
		nums = nums[:len(nums)-count+2]
	}
	return len(nums)
}

func main() {
	var arr = []int{1, 2, 2, 2}
	println(removeDuplicates(arr))
	arr = []int{1, 1, 1, 2, 2, 3}
	println(removeDuplicates(arr))
	arr = []int{0, 0, 1, 1, 1, 1, 2, 3, 3}
	println(removeDuplicates(arr))
	println(removeDuplicates([]int{1, 1, 1, 1, 2, 2, 2, 3, 3, 3}))
}
